<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" @click="dialogVisible = true">添加</el-button>
    </div>
    <el-table :data="listData" style="width: 100%" border>
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="strValue" label="设备分辨率">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-row :gutter="10">
              <el-col :span="16">
                <el-input v-model="row.strValue" class="edit-input" size="small" />
              </el-col>
              <el-button
                class="cancel-btn"
                size="small"
                icon="el-icon-refresh"
                type="warning"
                @click="cancelEdit(row)"
              >取消</el-button>
            </el-row>
          </template>
          <span v-else>{{ row.strValue }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="strCode" label="参数码" />
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="{row,$index}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >确认</el-button>
          <el-button size="mini" type="primary" @click="row.edit=!row.edit">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-show="total>0"
      :total="total"
      :page.sync="pagenum"
      :limit.sync="limit"
      :layout="layout"
      @pagination="getList"
    />

    <el-dialog
      title="新增分辨率"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="dialogForm"
        :model="form"
        :rules="rules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item prop="strValue" label="设备分辨率">
          <el-input v-model="form.strValue" placeholder="请输入设备分辨率" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleCancel('dialogForm')">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSysList, addTag, deleteSysParam, updateSys } from '../../api/addTag'
import Pagination from '@/components/Pagination'
import { validResolute } from '../../utils/validate'
export default {
  name: 'Resolving',
  components: { Pagination },
  data() {
    var validateValue = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入设备分辨率'))
      } else if (!validResolute(value)) {
        callback(new Error('请输入1~9个数字'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      total: 0,
      limit: 5,
      pagenum: 1,
      currentpage: 1,
      layout: 'total, prev, pager, next, jumper',
      form: {
        strValue: '',
        strName: 'ResolvingPower',
        strDesc: '设备分辨率',
        iType: '2'
      },
      listData: [],
      rules: {
        strValue: [{ required: true, validator: validateValue, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getSysList({ pagenum: this.pagenum, iType: 2 }).then(res => {
        this.total = res.total
        this.listData = res.data.map(v => {
          this.$set(v, 'edit', false)
          v.originalValue = v.strValue
          return v
        })
      })
    },
    handleDelete(row, index) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        deleteSysParam({ idInfo: [row.id] })
          .then(res => {
            this.$notify({
              title: '操作成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
          .catch(err => {
            console.log(err)
          })
      })
    },
    cancelEdit(row) {
      row.strValue = row.originalValue
      row.edit = false
    },
    confirmEdit(row) {
      if (!validResolute(row.strValue)) {
        this.$message({
          type: 'warning',
          message: '请输入1~9个数字'
        })
        return false
      }
      row.edit = false
      row.originalTitle = row.title
      updateSys({
        id: row.id,
        strName: row.strName,
        strValue: row.strValue,
        strDesc: row.strDesc,
        iType: row.iType
      }).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      })
    },
    handleCancel() {
      this.dialogVisible = false
      this.$refs['dialogForm'].resetFields()
    },
    handleConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addTag(this.form)
            .then(res => {
              this.dialogVisible = false
              this.$refs['dialogForm'].resetFields()
              this.getList()
              this.$message({
                title: 'Success',
                message: '添加成功',
                type: 'success'
              })
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
